<?php $this->_Hhead()?>
<link href="/Public/Home/public/common.css" rel="stylesheet" type="text/css">
<link href="/Public/Home/public/all_common.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/Public/Home/public/flow.js"></script>
<div class="container clearfix">
    <div class="shop-cart-box-hd">
        <h2 class="title">我的购物车</h2>
    </div>
    <div class="navbar">
        <span class="on">1.我的购物车</span>
        <span>2.填写核对订单信息</span>
        <span>3.成功提交订单</span>
    </div>
    <div class="shop-cart-box-bd">

        <table class="shop-cart-goods" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th class=" col-1">商品名称</th>
                <th class=" col-2">价格</th>
                <th class=" col-2">购买数量</th>
                <th class=" col-2">小计</th>
                <th class=" col-2">操作</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach($goods as $v){?>
            <tr>
                <td class="col col-1">
                    <div class="g-pic" style="width: 0px;"><input class="che" dat="<?php echo $v['id']?>" type="checkbox"></div>
                    <div class="g-pic">
                        <a href="<?php echo '/index.php?C=Index&F=goods_msg&id='.$v['g_id']?>" target="_blank">
                            <img src="<?php echo substr(strrchr($v['img'], '|'),1)?>" border="0" title="">
                        </a>
                    </div>
                    <div class="g-info">
                        <div class="g-name">
                            <a href="<?php echo '/index.php?C=Index&F=goods_msg&id='.$v['g_id']?>" target="_blank">
                                <?php echo $v['name']?> | 规格:<?php echo $v['gname']?>
                            </a>
                        </div>
                    </div>
                </td>
                <td class="col col-2">
                    ￥ <b id="jg<?php echo $v['id']?>"><?php echo $v['money']+$v['gmoney']?></b>
                </td>
                <td class="col col-2">
                    <div class="change-goods-num clearfix ">
                        <a href="javascript:void(0)" class="DelNum nu" add="jian" dat="<?php echo $v['id']?>">
                            -
                        </a>

                        <input tyep="text" name="number" value="<?php echo $v['num']?>" autocomplete="off"
                               data-gid="996543" class="goods-num" kc="<?php echo $v['kc']?>"
                               id="num<?php echo $v['id']?>">

                        <a href="javascript:void(0)" class="AddNum nu" add="jia" dat="<?php echo $v['id']?>">
                            +
                        </a>
                    </div>

                </td>
                <td class="col col-2">
                    <em id="goods_subtotal_996543">
                        ￥ <b id="xj<?php echo $v['id']?>"><?php echo ($v['money']+$v['gmoney'])*$v['num']?></b>
                    </em>

                </td>
                <td class="col col-2">
                    <a class="del" sid="<?php echo $v['id']?>"  href="javascript:void(0)">X</a>
                </td>
            </tr>
            <?php }?>
            </tbody>
        </table>
        <div class="total">
            总金额：<span id="money">￥ <b id="zje">0.00</b></span>元
        </div>
        <div class="go-next clearfix">
            <a class="btn btn-back " href="/index.php"> 继续购物 </a>&nbsp;&nbsp;
            <a class="btn btn-primary" id="qjs">去结账</a>
        </div>
    </div>
</div>

<script>
    $(function () {
        //去结算功能
        $('#qjs').bind('click',function(){
            var id='';
           //获取选中的商品，如果没有不能结算
            if($('.che').is(":checked")) {
                $('.che').each(function (i, t) {
                    if ($(this).is(":checked")) {
                        //得到对应小计
                        id += ',' + $(this).attr('dat');// 数据的id
                    }
                });
                location.href = '/index.php?C=Order&F=add&id=' + id;
            }else{
                $.get('/index.php?C=Order&F=add&id=' + id,function(msg){
                    if(msg.type=='1'){
                        alert(msg.msg);
                    }
                },'json')
            }
        });

        //删除数据
        $('.del').bind('click',function(){
            if(confirm('是否删除！')){
                var id = $(this).attr('sid');
                //发送ajax
                $.get("/index.php?C=Order&F=flowUpd",{id:id,type:'del'},function(abc){
                        alert(abc.msg);
                        if(abc.type==2){
                            location.href="/index.php?C=Order&F=flow";
                        }
                },'json')
            }
        });

        $('.nu').bind('click', function () {

                //数量对应的减一或者加一
                var add = $(this).attr('add'); // 判断操作属性
                var dat = $(this).attr('dat');  //数据id
                var kc = Number($('#num' + dat).attr('kc'));  //库存
                var jg = Number($('#jg' + dat).html()); //价格
                if (add == 'jia') {
                    //加一操作
                    var num = Number($('#num' + dat).val()) + 1 > kc ? kc : Number($('#num' + dat).val()) + 1; //不能超过库存
                    $('#num' + dat).val(num);

                    //改变小计
                    $('#xj' + dat).html((jg * num).toFixed(2));
                } else {
                    //减一操作
                    var num = Number($('#num' + dat).val()) - 1 > 0 ? Number($('#num' + dat).val()) - 1 : 1;
                    $('#num' + dat).val(num);

                    //改变小计
                    $('#xj' + dat).html((jg * num).toFixed(2));
                }

//            发送ajax改变数据库数据
                $.get("/index.php?C=Order&F=flowUpd", {id: dat, add: add, type: 'upd',kc:kc}, function (abc) {
                }, 'json')

        })


        //改变总金额
        $('.che').bind('click',function(){
            //获取所有选中的 多选框对应的小计
            var money=0;
            $('.che').each(function(i,t){
                if($(this).is(":checked")){
                    //得到对应小计
                    var id =$(this).attr('dat')
                    money+=Number($('#xj'+id).html());
                }
            });
            //改变总金额
            $('#zje').html(money.toFixed(2))
        })
    })

</script>
<?php $this->_Hfooter()?>